<?php echo $this->render('header.phtml'); ?>
<label class="title">Quản lý hàng hóa - Chỉnh sửa thông tin hàng hóa rao mua</label>
<div class="horiz_sep">&nbsp;</div>
<?php
	$commodity = (object) $this->commodity;
	$com_categories = (object) $this->com_categories;
	if (isset($this->errors)){
		$errors = $this->errors;
		if (key_exists("not_enough_money", $errors)) $not_enough_money = $errors['not_enough_money'];
		if (!isset($not_enough_money)){
			if (key_exists("name_null", $errors)) $name_null = $errors['name_null'];
			if (key_exists("status_null", $errors)) $status_null = $errors['status_null'];
			if (key_exists("avail_null", $errors)) $avail_null = $errors['avail_null'];
			if (key_exists("price_null", $errors)) $price_null = $errors['price_null'];
			if (key_exists("category_null", $errors)) $category_null = $errors['category_null'];
		}	
	}
?>
<form action="<? echo $this -> baseUrl ?>/<?= $this->contextPath ?>/edit-buy" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?= $commodity->com_id; ?>"/>
<input type="hidden" name="posted_date" value="<?= $commodity->com_posted_date; ?>"/>
<table cellpadding="0" cellspacing="5" width="100%">
  	<tr>
    	<th width="20%">Name:</th>
    	<td>
    		<input maxlength="150" type="text" name="name" value="<?= $commodity->com_name; ?>" style="width: 250px;"/>
    		<?php if (isset($name_null)){ ?>
				<span style="color:red; font-weight:bold;"><?php echo $name_null; ?></span>
			<?php } ?>
    	</td>
  	</tr>  
  	<tr>
    	<th>Category:</th>
    	<td>
    		<?php if (isset($category_null)){ ?>
				<span style="color:red; font-weight:bold;"><?php echo $category_null; ?></span>
			<?php } ?>  	
    		<table cellpadding="0" cellspacing="0" width="100%">
    		<?php
    			if (isset($this->sections)) {
    				$sections = $this->sections;
    				foreach ($sections as $section){
    		?>
    			<tr>
    				<td>
    					<?= $section->sec_name; ?>
    				</td>
    			</tr>
    			<tr>
    				<td>
			    		<?php
			    			if (isset($this->categories)) {
			    				$i = 0;
			    				$count = count($this->categories);
			    				foreach ($this->categories as $category){
			    					if ($category->sec_id == $section->sec_id){ 
			    					if ($i%5 == 0)
			    						echo "<tr><td>";
			    					else 
			    						echo "</td><td>";
			    					$checked = "";
			    					foreach ($com_categories as $com_category) {
			    						if ($category->cat_id == $com_category->cat_id)
			    							$checked = "checked";
			    					}
				
			    		?>
			    			
						    		<input type="checkbox" name="category[]" value="<?= $category->cat_id; ?>" <?= $checked; ?>/><?= $category->cat_name; ?>
			    		<?php
			    				if ($i % 5 == 4)
			    					echo "</td></tr>";
			    				else if ($i==$count-1)
			    					echo "</td></tr>";
			    				$i++;
			    					}				
			    				}
			    			} 
    				}
    			}
			    		?>    		
    		</table>    			 		
    	</td>
  	</tr>	
  	<tr>
    	<th>Status:</th>
    	<td>
    		<select name="status">
    			<?php
    			if (isset($this->eway_status)) {
    				foreach ($this->eway_status as $status){
    					if ($status->sta_id == $commodity->com_status){
    			?>
    				<option value="<?= $status->sta_id; ?>" selected="selected"><?= $status->sta_name; ?></option>
    			<?php 	} else { ?>
    				<option value="<?= $status->sta_id; ?>"><?= $status->sta_name; ?></option>
    			<? 
    					}
    				} 
    			}
    			?>
    		</select>
    		<?php if (isset($status_null)){ ?>
				<span style="color:red; font-weight:bold;"><?php echo $status_null; ?></span>
			<?php } ?>
    	</td>
  	</tr>
  	<tr>
    	<th>Avail:</th>
    	<td>    	
    		<select name="avail">    		
    			<option value="2" selected="selected">-- Chọn --</option>
    			<option value="1" <?php if ($commodity->com_avail == 1) echo "selected"; ?>>Còn hàng</option>
    			<option value="0" <?php if ($commodity->com_avail == 0) echo "selected"; ?>>Hết hàng</option>
    		</select>
    		<?php if (isset($avail_null)){ ?>
				<span style="color:red; font-weight:bold;"><?php echo $avail_null; ?></span>
			<?php } ?>
    	</td>
  	</tr>
  	<tr>
    	<th>Price:</th>
    	<td>
    		<input maxlength="200" type="text" name="price" value="<?= $commodity->com_price; ?>" style="width: 250px;"/>
    		<?php if (isset($price_null)){ ?>
				<span style="color:red; font-weight:bold;"><?php echo $price_null; ?></span>
			<?php } ?>
    	</td>
  	</tr>
  	<tr>
    	<th>Vip:</th>
    	<td>
    		<input type="checkbox" name="vip" value="1" <?php if ($commodity->com_vip == 1) echo "checked"; ?>/>
    	</td>
  	</tr>
  	<tr>
    	<th colspan="2">Description:</th>
    </tr>
    <tr>
    	<td colspan="2">
    		<textarea name="description" rows="5" style="width:100%;"><?= $commodity->com_description; ?></textarea>
			<script src="<?= $this->baseUrl ?>/public/scripts/fckeditor/fckeditor.js" type="text/javascript"></script>	
			<script type="text/javascript">
				<!--
				// Automatically calculates the editor base path based on the _samples directory.
				// This is usefull only for these samples. A real application should use something like this:
				// oFCKeditor.BasePath = '/fckeditor/' ;	// '/fckeditor/' is the default value.
				var sBasePath = '<?= $this->baseUrl ?>/public/scripts/fckeditor/' ;
				var oFCKeditor = new FCKeditor( 'description' ) ;
				oFCKeditor.BasePath	= sBasePath ;
				oFCKeditor.ToolbarSet = 'Member';
				oFCKeditor.Height	= 250 ;
				oFCKeditor.ReplaceTextarea() ;
				//-->
			</script>
    	</td>
  	</tr>
  	<tr>
  		<td>Image:</td>
  	</tr>
  	<?php for ($i=0; $i<5; $i++){ ?>
  		<tr>
	  		<td>
	  			<input type="file" name="image_file[]" width="50%"/>
	  		</td>
	  	</tr>
  	<?php }?> 
  	<tr>    	
    	<td align="left" colspan="2">
    		<input type="submit" name="save" value="Save"/>
    	</td>
  	</tr>
</table>
</form>
<?php echo $this->render('footer.phtml');?> 